package cn.com.pxto.dao;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.pxto.commons.SearchParam;
import cn.com.pxto.dao.pagesupport.hibernate.HibernatePageDaoSupport;
import cn.com.pxto.model.Dic;

/**
 * 
 * Copyright (C) 2006 Bejing iActive Tec Co.,Ltd File: DicDAO.java
 * 
 * file description
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @author Gauss Cheung 2006-7-20
 */

public class DicDAO extends HibernatePageDaoSupport {
	/**
	 * 
	 * @param dic
	 */
	public void modifyDic(Dic dic){
		getTemplate().saveOrUpdate(dic);
	}
	/**
	 * 
	 * @param id
	 */
	public void deleteDic(Integer id){
		Object o = getTemplate().get(Dic.class,id);
		if(o!=null){
			getTemplate().delete(o);
		}
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	public Dic getDic(Integer id){
		return (Dic)getTemplate().get(Dic.class,id);
	}
	
	/**
	 * ����ֵ�����ɸѡ��Ҫ���ֵ�
	 * @param type String �ֵ�����
	 * @return List
	 */
	public List getDicsByType(String type){
		String HQL = "SELECT dic FROM Dic as dic where dic.type = ? order by dic.defValue,dic.id";
		return getHibernateTemplate().find(HQL,type);
	}
	/**
	 * ����ֵ����ͺ;���idֵ��ȡ�ֵ����
	 * @param type String����
	 * @param id intֵ
	 * @return Dic����
	 */
	public Dic getDicByTypeAndID(String type,Integer id){
		Dic result = null;
		Log log=LogFactory.getLog(getClass());
		log.info(type+id);
		String hql = "SELECT dic FROM Dic as dic where dic.type = ? and dic.dicId= ? ";
		Object[] values = {type,id};
		List list = getHibernateTemplate().find(hql,values);

		log.info(list);
		Iterator iter = list.iterator();
		if (iter.hasNext()) result = (Dic)iter.next();
		return result;
	}
	/**
	 * ����ֵ����ͺ�Name��ȡ�ֵ����
	 * @param type
	 * @param Name
	 * @return
	 */
	public Dic getDicByTypeAndName(String type,String Name){
		Dic result = null;
		Log log=LogFactory.getLog(getClass());
		log.info(type+Name);
		String hql = "SELECT dic FROM Dic as dic where dic.type = ? and dic.name= ? ";
		Object[] values = {type,Name};
		List list = getHibernateTemplate().find(hql,values);

		log.info(list);
		Iterator iter = list.iterator();
		if (iter.hasNext()) result = (Dic)iter.next();
		return result;
	}
	/**
	 * ����Dic
	 * @param params
	 * @param pageInfo
	 * @return
	 */
	public List listDic(SearchParam[] params){
		StringBuffer HQL = new StringBuffer(
		"select d from Dic as d where (1=1)");
		HQL.append(SearchParam.getSimpleWhereSentence(params,"d"));
		HQL.append(" order by d.id");
		return getHibernateTemplate().find(HQL.toString(), SearchParam.getSearchValues(params));
		}
	/**
	 * ��ݶ��dicId��ѯ
	 * @param params
	 * @param strId
	 * @return
	 */
	public List listDicById(SearchParam[] params,String strId){
		StringBuffer HQL = new StringBuffer(
		"select d from Dic as d where (1=1)");
		HQL.append(SearchParam.getSimpleWhereSentence(params,"d"));
		HQL.append(" and d.id IN (");
		HQL.append(strId);
		HQL.append(")");
		HQL.append(" order by d.id");
		return getHibernateTemplate().find(HQL.toString(),SearchParam.getSearchValues(params));
	}
	/**
	 * 
	 * @param type
	 * @return
	 */
	public List getDicsByTypeForFile(String type){
		String HQL = "SELECT dic FROM Dic as dic where dic.type = ? and dic.id <> '0' order by dic.defValue,dic.id";
		return getHibernateTemplate().find(HQL,type);
	}
	public int getMaxIdForChannelType(String classSid){
		String HQL = "select max(d.id) from Dic as d where d.type=?";
		List list = getHibernateTemplate().find(HQL,classSid);
		Integer id = null;
		if(list!=null&&list.size()>0){
			Iterator it = list.iterator();
			id = (Integer)it.next();
		}
		
		return id.intValue();
	}
}
